﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PotentialRentals.Models;
using PotentialRentals.DAL;

namespace PotentialRentals.Repositories
{
    public class RentalRepository
    {
        private RentalDBContext _db;

        public RentalRepository(RentalDBContext db)
        {
            _db = db ?? new RentalDBContext();
        }

        public IEnumerable<Rental> GetAll() 
        {
            return _db.Rentals.Include("Address").Include("Type").Include("Amenities").AsEnumerable();
        }

        public Rental GetRental(int id)
        {
            return _db.Rentals.Include("Amenities").Include("Address").Include("Address.State").Include("Address.Amenities").Include("Type").FirstOrDefault(x => x.RentalId == id);
        }

        public void Add(Rental rental)
        {
            //check for existing?

            _db.Rentals.Add(rental);
        }

        public void Delete(int rentalId)
        {
            var existingRental = _db.Rentals.Single(x => x.RentalId == rentalId);
            _db.Rentals.Remove(existingRental);
        }
    }
}